package com.example.jegotrip.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toolbar;

import com.example.jegotrip.R;
import com.example.jegotrip.base.BaseActivity;
import com.example.jegotrip.db.CartDBHelper;
import com.example.jegotrip.entity.ProductInfo;
import com.example.jegotrip.entity.UserInfo;
import com.example.jegotrip.utils.CustomImageView;
import com.example.jegotrip.utils.CustomToast;

import java.util.Objects;

public class ProductDetailsActivity extends BaseActivity {
    private ProductInfo productInfo;
    private UserInfo userInfo;
    private Toolbar toolbar;
    private CustomImageView product_img;
    private TextView product_title;
    private TextView product_details;
    private TextView product_price;
    private Button add_cart;

    @SuppressLint("SetTextI18n")
    @Override
    protected void initData() {
        userInfo = UserInfo.getUserInfo();
        productInfo = (ProductInfo) Objects.requireNonNull(getIntent().getExtras()).get("product_info");
        if( productInfo != null){
            product_img.setImageURL(productInfo.getProduct_img());
            product_price.setText(productInfo.getProduct_price() + "");
            product_title.setText(productInfo.getProduct_title());
            product_details.setText(productInfo.getProduct_details());
        }
    }

    @Override
    protected void setListener() {
        toolbar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        add_cart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                CartInfo cartInfo = CartDBHelper.getInstance(ProductDetailsActivity.this).isAddCart(productInfo.getProduct_id());
//                if (cartInfo.getProduct_count() > 0){
//                    CartDBHelper.getInstance(ProductDetailsActivity.this).updateProduct(cartInfo.getCart_id(),cartInfo);
//                } else {

                    int res = CartDBHelper.getInstance(ProductDetailsActivity.this).createCart(
                            productInfo.getProduct_id(),
                            productInfo.getProduct_img(),
                            productInfo.getProduct_title(),
                            productInfo.getProduct_price(),
                            1);
                if (res > 0){
                    CustomToast.showCenterToast(ProductDetailsActivity.this,"添加成功~");
                    Intent intent = new Intent(ProductDetailsActivity.this, ShoppingCartActivity.class);
                    startActivity(intent);
                } else {
                    CustomToast.showCenterToast(ProductDetailsActivity.this,"添加失败~");
                }
            }
        });
    }

    @Override
    protected void initViews() {
        toolbar = findViewById(R.id.toolBar);
        product_img = findViewById(R.id.pv_product_img);
        product_title = findViewById(R.id.pv_product_title);
        product_details = findViewById(R.id.pv_product_details);
        product_price = findViewById(R.id.pv_product_price);
        add_cart = findViewById(R.id.add_cart);
    }

    @Override
    protected int getLayoutResOId() {
        return R.layout.activity_product_details;
    }
}
